
<!-- 设备编号下拉框组件 -->

<template>
    <el-select v-model="searchForm[data['prop']]" :placeholder="data['placeholder']" multiple collapse-tags clearable popper-class="vehicle-number-select" style="width: 180px">
        <el-checkbox v-model="checkAll" :indeterminate="indeterminate" @change="handleCheckAll">全部</el-checkbox>
        <el-option v-for="item in data.options" :key="`${item.vehicleNo}_${item.vin}`" :label="item.vehicleNo" :value="item.vin"></el-option>
    </el-select>
</template>

<script setup>
import { ref, watch } from 'vue';

const checkAll = ref(false);
const indeterminate = ref(false);

const props = defineProps({
    data: {
        type: Object,
        default: () => {}
    }
});

const searchForm = defineModel('searchForm');

watch(() => searchForm.value[props.data['prop']], (val) => {
    if(val.length === 0) {
        checkAll.value = false;
        indeterminate.value = false;
    }else if(val.length === props.data['options'].length){
        checkAll.value = true;
        indeterminate.value = false;
    }else{
        indeterminate.value = true;
    }
});

// 全选、取消全选
const handleCheckAll = (val) => {
    indeterminate.value = false;
    if(val){
        searchForm.value[props.data['prop']] = props.data['options'].map((item) => item.vin);
    }else{
        searchForm.value[props.data['prop']] = [];
    }
};
</script>

<style lang="scss">
@import './SelectNumber.scss'
</style>